//
//  YunFoodPhotoVC.swift
//  YunChenCalculator
//
//  Created by Li Dong on 2023/10/17.
//

import UIKit

class YunFoodPhotoVC: YunBaseVC {
    
    @IBOutlet weak var addPhotoBtn: UIButton!
    @IBOutlet weak var showView: UIView!
    @IBOutlet weak var photoView: UIImageView!
    @IBOutlet weak var queryBtn: UIButton!
    var selectImage: UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "食物营养查询"
        view.backgroundColor = .init(hex: 0xE0FFD1)
  
    }


    @IBAction func addPhotoHandler(_ sender: UIButton) {
        let picker = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let action1 = UIAlertAction(title: "相机", style: .default) { _ in
            PermissionUtils.albumAndCamera(type: .camera) {[weak self] in
                guard let self = self else {
                    return
                }
                self.openImagePicker(type: .camera)
            }
        }
        let action2 = UIAlertAction(title: "相册", style: .default) { _ in
            PermissionUtils.albumAndCamera(type: .photoLibrary) {[weak self] in
                guard let self = self else {
                    return
                }
                self.openImagePicker(type: .photoLibrary)
            }
        }
        let action3 = UIAlertAction(title: "取消", style: .cancel)
        picker.addAction(action1)
        picker.addAction(action2)
        picker.addAction(action3)
        present(picker, animated: true)
    }
    
    
    @IBAction func queryHandler(_ sender: Any) {
        guard let tempImage = selectImage else {
            return
        }
        showHUD()
        APICenter.foodQueryRequest(image: tempImage) {[weak self] model in
            self?.dismissHUD()
            guard let self = self else {
                return
            }
            guard let tempModel = model else {
                return
            }
            self.navigationController?.pushViewController(YunFoodResultVC(model: tempModel), animated: true)
        } fail: {[weak self] msg in
            self?.dismissHUD()
            guard let self = self else {
                return
            }
            self.showHUDTextOnly(msg)
        }

    }
    
    private func openImagePicker(type: UIImagePickerController.SourceType) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = type
        present(picker, animated: true)
    }
}

extension YunFoodPhotoVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)
        guard let image = info[.originalImage] as? UIImage else {
            return
        }
        selectImage = image
        photoView.image = selectImage
        showView.isHidden = false
        addPhotoBtn.isHidden = true
        queryBtn.isHidden = false
    }
}
